/**
 * 基本上，當使用者關閉瀏覽器時，Cookie 就立刻消失了。<br>
 * 但是它們也可以設定在指定的日期之前，每次打開瀏覽時，這些 Cookie 都還是持續存在。<br>
 * <br>
 * 為了安全理由，Cookie 只可以被特定的伺服器所讀取。<br>
 * PS: 在這裏特定的伺服器是指當初設定 cookie 的伺服器或位在相同網或的其它伺服器<br>
 * <br>
 * 此外更可進一步限制 Cookie ，讓有存取資格的伺服器中的某個 URL 目錄階層才能存取<br>
 */

package cookie.basic;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.e885.log.E885Logger;

/**
 * url: http://127.0.0.1:8080/JSP/cookie/basicAdd<br>
 * 產生一個 cookie, 並設定它。<br>
 * 注意：但只要 Browser 關閉，那麼 Cookie 就會消失。
 * 
 * @author Grady.Kao
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2000-2008
 */

public class CookieAdder extends HttpServlet
{
	private static final long serialVersionUID = -2053909949817327491L;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void service( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
	{
		E885Logger.testLog.debug( "execute CookieAdder.service() to add cookie." );
		
		Cookie cookie1 = new Cookie( "BasicCookie-001", "Graydy.Kao.001" );
		Cookie cookie2 = new Cookie( "BasicCookie-002", "Graydy.Kao.002" );
		Cookie cookie3 = new Cookie( "BasicCookie-003", "Graydy.Kao.003" );

		response.addCookie( cookie1 );
		response.addCookie( cookie2 );
		response.addCookie( cookie3 );
		
		response.setContentType( "text/html; charset=UTF-8" );
		PrintWriter writer = response.getWriter();
		writer.println( "Add Cookie 已經完成" );
		writer.close();
	}
}

